{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "d92b1e17-23db-49ac-accc-ab76b373359b",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(491, 5)\n",
      "(491, 4)\n",
      "(123, 5)\n",
      "(123, 4)\n",
      "Epoch 1/40\n",
      "123/123 [==============================] - 1s 5ms/step - loss: 1.2760 - accuracy: 0.4440 - val_loss: 1.0998 - val_accuracy: 0.5203\n",
      "Epoch 2/40\n",
      "123/123 [==============================] - 1s 4ms/step - loss: 0.9995 - accuracy: 0.5886 - val_loss: 0.8039 - val_accuracy: 0.7073\n",
      "Epoch 3/40\n",
      "123/123 [==============================] - 1s 4ms/step - loss: 0.8261 - accuracy: 0.6884 - val_loss: 0.7817 - val_accuracy: 0.6667\n",
      "Epoch 4/40\n",
      "123/123 [==============================] - 0s 4ms/step - loss: 0.7562 - accuracy: 0.6823 - val_loss: 0.7656 - val_accuracy: 0.7154\n",
      "Epoch 5/40\n",
      "123/123 [==============================] - 0s 4ms/step - loss: 0.6817 - accuracy: 0.7230 - val_loss: 0.7063 - val_accuracy: 0.7805\n",
      "Epoch 6/40\n",
      "123/123 [==============================] - 0s 4ms/step - loss: 0.6049 - accuracy: 0.7678 - val_loss: 0.5435 - val_accuracy: 0.8293\n",
      "Epoch 7/40\n",
      "123/123 [==============================] - 0s 4ms/step - loss: 0.5487 - accuracy: 0.7841 - val_loss: 0.5889 - val_accuracy: 0.7967\n",
      "Epoch 8/40\n",
      "123/123 [==============================] - 1s 4ms/step - loss: 0.5311 - accuracy: 0.7821 - val_loss: 0.6546 - val_accuracy: 0.7967\n",
      "Epoch 9/40\n",
      "123/123 [==============================] - 0s 4ms/step - loss: 0.5048 - accuracy: 0.8045 - val_loss: 0.5954 - val_accuracy: 0.8455\n",
      "Epoch 10/40\n",
      "123/123 [==============================] - 1s 4ms/step - loss: 0.4442 - accuracy: 0.8208 - val_loss: 0.5179 - val_accuracy: 0.8618\n",
      "Epoch 11/40\n",
      "123/123 [==============================] - 0s 4ms/step - loss: 0.4433 - accuracy: 0.8350 - val_loss: 0.4864 - val_accuracy: 0.8618\n",
      "Epoch 12/40\n",
      "123/123 [==============================] - 1s 4ms/step - loss: 0.3827 - accuracy: 0.8289 - val_loss: 0.5120 - val_accuracy: 0.8374\n",
      "Epoch 13/40\n",
      "123/123 [==============================] - 0s 4ms/step - loss: 0.4581 - accuracy: 0.8350 - val_loss: 0.4971 - val_accuracy: 0.8618\n",
      "Epoch 14/40\n",
      "123/123 [==============================] - 0s 4ms/step - loss: 0.3769 - accuracy: 0.8534 - val_loss: 0.4214 - val_accuracy: 0.8780\n",
      "Epoch 15/40\n",
      "123/123 [==============================] - 0s 4ms/step - loss: 0.3911 - accuracy: 0.8493 - val_loss: 0.4238 - val_accuracy: 0.8699\n",
      "Epoch 16/40\n",
      "123/123 [==============================] - 0s 4ms/step - loss: 0.3525 - accuracy: 0.8574 - val_loss: 0.4540 - val_accuracy: 0.8699\n",
      "Epoch 17/40\n",
      "123/123 [==============================] - 0s 4ms/step - loss: 0.3249 - accuracy: 0.8859 - val_loss: 0.4078 - val_accuracy: 0.8780\n",
      "Epoch 18/40\n",
      "123/123 [==============================] - 0s 4ms/step - loss: 0.3333 - accuracy: 0.8554 - val_loss: 0.5100 - val_accuracy: 0.8211\n",
      "Epoch 19/40\n",
      "123/123 [==============================] - 0s 4ms/step - loss: 0.3276 - accuracy: 0.8758 - val_loss: 0.3943 - val_accuracy: 0.8780\n",
      "Epoch 20/40\n",
      "123/123 [==============================] - 0s 4ms/step - loss: 0.2691 - accuracy: 0.8941 - val_loss: 0.4635 - val_accuracy: 0.8374\n",
      "Epoch 21/40\n",
      "123/123 [==============================] - 1s 4ms/step - loss: 0.2970 - accuracy: 0.8880 - val_loss: 0.4657 - val_accuracy: 0.8699\n",
      "Epoch 22/40\n",
      "123/123 [==============================] - 0s 4ms/step - loss: 0.3175 - accuracy: 0.8921 - val_loss: 0.4921 - val_accuracy: 0.8699\n",
      "Epoch 23/40\n",
      "123/123 [==============================] - 0s 4ms/step - loss: 0.2811 - accuracy: 0.8900 - val_loss: 0.4082 - val_accuracy: 0.8943\n",
      "Epoch 24/40\n",
      "123/123 [==============================] - 0s 4ms/step - loss: 0.2632 - accuracy: 0.8941 - val_loss: 0.5130 - val_accuracy: 0.8780\n",
      "Epoch 25/40\n",
      "123/123 [==============================] - 0s 4ms/step - loss: 0.2713 - accuracy: 0.8982 - val_loss: 0.4436 - val_accuracy: 0.9024\n",
      "Epoch 26/40\n",
      "123/123 [==============================] - 0s 4ms/step - loss: 0.2185 - accuracy: 0.9145 - val_loss: 0.3912 - val_accuracy: 0.9024\n",
      "Epoch 27/40\n",
      "123/123 [==============================] - 1s 4ms/step - loss: 0.2515 - accuracy: 0.9145 - val_loss: 0.3581 - val_accuracy: 0.9187\n",
      "Epoch 28/40\n",
      "123/123 [==============================] - 0s 4ms/step - loss: 0.2192 - accuracy: 0.9226 - val_loss: 0.4085 - val_accuracy: 0.9106\n",
      "Epoch 29/40\n",
      "123/123 [==============================] - 0s 4ms/step - loss: 0.2157 - accuracy: 0.9104 - val_loss: 0.4437 - val_accuracy: 0.9024\n",
      "Epoch 30/40\n",
      "123/123 [==============================] - 0s 4ms/step - loss: 0.1905 - accuracy: 0.9206 - val_loss: 0.4512 - val_accuracy: 0.8943\n",
      "Epoch 31/40\n",
      "123/123 [==============================] - 0s 4ms/step - loss: 0.2308 - accuracy: 0.9022 - val_loss: 0.4897 - val_accuracy: 0.8780\n",
      "Epoch 32/40\n",
      "123/123 [==============================] - 1s 4ms/step - loss: 0.2272 - accuracy: 0.9002 - val_loss: 0.3540 - val_accuracy: 0.9187\n",
      "Epoch 33/40\n",
      "123/123 [==============================] - 0s 4ms/step - loss: 0.1434 - accuracy: 0.9409 - val_loss: 0.3512 - val_accuracy: 0.9106\n",
      "Epoch 34/40\n",
      "123/123 [==============================] - 0s 4ms/step - loss: 0.2161 - accuracy: 0.9043 - val_loss: 0.4068 - val_accuracy: 0.9024\n",
      "Epoch 35/40\n",
      "123/123 [==============================] - 0s 4ms/step - loss: 0.1781 - accuracy: 0.9389 - val_loss: 0.3648 - val_accuracy: 0.9512\n",
      "Epoch 36/40\n",
      "123/123 [==============================] - 0s 4ms/step - loss: 0.1819 - accuracy: 0.9409 - val_loss: 0.4619 - val_accuracy: 0.9106\n",
      "Epoch 37/40\n",
      "123/123 [==============================] - 0s 4ms/step - loss: 0.1659 - accuracy: 0.9348 - val_loss: 0.4168 - val_accuracy: 0.9106\n",
      "Epoch 38/40\n",
      "123/123 [==============================] - 0s 4ms/step - loss: 0.1709 - accuracy: 0.9389 - val_loss: 0.3759 - val_accuracy: 0.9350\n",
      "Epoch 39/40\n",
      "123/123 [==============================] - 0s 4ms/step - loss: 0.1049 - accuracy: 0.9633 - val_loss: 0.3635 - val_accuracy: 0.9593\n",
      "Epoch 40/40\n",
      "123/123 [==============================] - 1s 4ms/step - loss: 0.1257 - accuracy: 0.9532 - val_loss: 0.4628 - val_accuracy: 0.8699\n",
      "4/4 [==============================] - 0s 2ms/step - loss: 0.4628 - accuracy: 0.8699\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "from autopilot_data import AutopilotData\n",
    "# from sklearn.model_selection import train_test_split\n",
    "\n",
    "input_num = 5 #输入值个数\n",
    "classes_num = 4 #分类个数\n",
    "input_shape = (input_num)\n",
    "\n",
    "ad = AutopilotData()\n",
    "x,y = ad.load_data()\n",
    "\n",
    "# x_train,x_test,y_train,y_test= train_test_split(x,y,test_size=0.2,random_state = 20,shuffle = True)\n",
    "x_train,y_train,x_test,y_test = ad.split_data(x,y,train_rate=0.8)\n",
    "# x_train,x_test,y_train,y_test= x,x,y,y\n",
    "print(x_train.shape)\n",
    "print(y_train.shape)\n",
    "print(x_test.shape)\n",
    "print(y_test.shape)\n",
    "\n",
    "x_train, x_test = x_train / 500.0, x_test / 500.0\n",
    "y_train, y_test = y_train / 1.0, y_test / 1.0\n",
    "\n",
    "model = tf.keras.models.Sequential()\n",
    "model.add(tf.keras.layers.Dense(1024,input_shape=(5,), activation='relu'))\n",
    "model.add(tf.keras.layers.Dense(128, activation='relu'))\n",
    "model.add(tf.keras.layers.Dense(32, activation='relu'))\n",
    "model.add(tf.keras.layers.Dense(classes_num, activation='softmax'))\n",
    "\n",
    "model.compile(optimizer='adam',\n",
    "              loss='categorical_crossentropy',\n",
    "              metrics=['accuracy'])\n",
    "callbacks = [tf.keras.callbacks.TensorBoard('./keras')]\n",
    "model.fit(x_train, y_train, epochs=50, batch_size=4, verbose=1, validation_data=(x_test, y_test), callbacks=callbacks)\n",
    "model.evaluate(x_test, y_test, batch_size=None, verbose=1, sample_weight=None, steps=None,callbacks=None, max_queue_size=10, workers=1, use_multiprocessing=False,return_dict=False)\n",
    "\n",
    "model.save('model.h5')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e222c963-72f9-4afc-9e61-438c03102a0e",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7321727f-cd45-4b80-b088-5ea9aefb01ed",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "adeab07c-cc1f-4882-a6fa-ab233f6085cb",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.15"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
